package com.zy.quartz.task;

import com.zy.business.domain.TbPunchCard;
import com.zy.business.mapper.HolidayMapper;
import com.zy.business.mapper.PunchCardMapper;
import com.zy.business.service.HolidayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: 定时更新迟到和缺卡状态
 * @author: whf
 * @create: 2023-02-09 16:56
 **/
@Component("lateAndLackTask")
public class LateAndLackTask {

    @Autowired
    private HolidayMapper holidayMapper;

    @Autowired
    private HolidayService holidayService;

    @Autowired
    private PunchCardMapper punchCardMapper;

    /**
     * 早上上班迟到
     */
    public void late1() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            List<TbPunchCard> punchCards = punchCardMapper.getByStatus(0, 1,null);
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    if (punchCard.getStatus()==0) {
                        punchCard.setStatus(2);
                        punchCard.setUpdateTime(new Date());
                        punchCardMapper.updateById(punchCard);
                    }
                }
            }
        }
    }

    /**
     * 早上上班缺卡
     */
    public void lack1() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            List<TbPunchCard> punchCards = punchCardMapper.getMissCard(1);
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    punchCard.setStatus(1);
                    punchCard.setUpdateTime(new Date());
                    punchCardMapper.updateById(punchCard);
                }
            }
        }
    }

    /**
     * 早上下班缺卡
     */
    public void lack2() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            List<TbPunchCard> punchCards = punchCardMapper.getByStatus(0, 2,null);
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    if (punchCard.getStatus()==0) {
                        punchCard.setStatus(1);
                        punchCard.setUpdateTime(new Date());
                        punchCardMapper.updateById(punchCard);
                    }
                }
            }
        }
    }

    /**
     * 下午上班迟到
     */
    public void late3() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            List<TbPunchCard> punchCards = punchCardMapper.getByStatus(0, 3,null);
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    if (punchCard.getStatus()==0) {
                        punchCard.setStatus(2);
                        punchCard.setUpdateTime(new Date());
                        punchCardMapper.updateById(punchCard);
                    }
                }
            }
        }
    }

    /**
     * 下午上班缺卡
     */
    public void lack3() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            List<TbPunchCard> punchCards = punchCardMapper.getMissCard( 3);
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    punchCard.setStatus(1);
                    punchCard.setUpdateTime(new Date());
                    punchCardMapper.updateById(punchCard);
                }
            }
        }
    }

    /**
     * 下午下班缺卡
     */
    public void lack4() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer flag = holidayMapper.getIsHoliday(sdf.format(new Date()));
        if(flag==null){
            flag = holidayService.createHoliday(new Date());
        }
        if(flag==1) {
            List<TbPunchCard> punchCards = punchCardMapper.getByStatus(0, 4,null);
            if (punchCards != null && punchCards.size() > 0) {
                for (TbPunchCard punchCard : punchCards) {
                    if (punchCard.getStatus()==0) {
                        punchCard.setStatus(1);
                        punchCard.setUpdateTime(new Date());
                        punchCardMapper.updateById(punchCard);
                    }
                }
            }
        }
    }
}
